package com.heima.article.controller.v1;

import com.heima.article.dtos.ApArticleConfigDto;
import com.heima.article.dtos.ApArticleDto;
import com.heima.article.dtos.ApArticleInfoDto;
import com.heima.article.pojos.ApArticle;
import com.heima.article.pojos.ApArticleConfig;
import com.heima.article.pojos.ArticleHomeDto;
import com.heima.article.service.ApArticleConfigService;
import com.heima.article.service.ApArticleService;
import com.heima.article.vos.ArticleInfoVo;
import com.heima.model.common.dtos.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/article")
@Slf4j
public class ArticleHomeController {

    @Autowired
    private ApArticleService articleService;

    @Autowired
    private ApArticleConfigService apArticleConfigService;

    /**
     * 加载首页
     *
     * @param articleHomeDto
     * @return
     */
    @PostMapping("/load")
    public ResponseResult load(@RequestBody ArticleHomeDto articleHomeDto) {
        List<ApArticle> articleList = articleService.loadArticleByDbOrRedis(articleHomeDto, "3");
        return ResponseResult.okResult(articleList);
    }

    /**
     * 加载更多
     *
     * @param articleHomeDto
     * @return
     */
    @PostMapping("/loadmore")
    public ResponseResult loadmore(@RequestBody ArticleHomeDto articleHomeDto) {
        List<ApArticle> articleList = articleService.loadArticle(articleHomeDto, "2");
        return ResponseResult.okResult(articleList);
    }

    /**
     * 加载最新
     *
     * @param articleHomeDto
     * @return
     */
    @PostMapping("/loadnew")
    public ResponseResult loadnew(@RequestBody ArticleHomeDto articleHomeDto) {
        List<ApArticle> articleList = articleService.loadArticle(articleHomeDto, "1");
        return ResponseResult.okResult(articleList);
    }

    /**
     * 发布文章至文章微服务
     *
     * @param apArticleDto 前端参数articleDto
     * @return 返回保存文章, 文章id
     */
    @PostMapping("/save")
    public ResponseResult<Long> save(@RequestBody ApArticleDto apArticleDto) {
        long articleId = articleService.saveOrUpdateArticle(apArticleDto);
        return ResponseResult.okResult(articleId);
    }

    /**
     * 删除app端文章
     *
     * @param articleId 文章id
     */
    //@DeleteMapping("/del_article")
    @GetMapping("/del_article")
    public ResponseResult delArticle(@RequestParam Long articleId) {
        articleService.delArticle(articleId);
        return ResponseResult.okResult(null);
    }

    /**
     * 上下架app端文章
     *
     * @param articleId 文章id
     * @param enable    是否上下架 0下架 1上架
     */
    @PutMapping("/downOrUp")
    public ResponseResult downOrUp(@RequestParam("articleId") Long articleId, @RequestParam("enable") Short enable) {
        articleService.downOrUp(articleId, enable);
        return ResponseResult.okResult(null);
    }

    /**
     * 某篇文章的行为状态回显 <br>
     * 状态回显:当用户对某一篇文章点赞 收藏 关注后,退出后再点击这篇文章把这些状态回显给前端
     *
     * @param apArticleInfoDto 前端dto
     * @return 返回结果
     */
    @PostMapping("/load_article_behavior")
    public ResponseResult<Map<String,Boolean>> loadArticleBehavior(@RequestBody ApArticleInfoDto apArticleInfoDto) {
        log.info("调用接口----某篇文章的行为状态回显:{}", apArticleInfoDto);
        Map<String,Boolean> loadData = articleService.loadArticleBehavior(apArticleInfoDto);
        return ResponseResult.okResult(loadData);
    }

    /**
     * 根据文章id 查询文章信息
     * @param articleId 文章id
     * @return 返回结果
     */
    @PostMapping("/getArticle")
    public ResponseResult<ApArticle> getArticle(@RequestBody Long articleId) {
        //参数校验
        if (articleId == null) {
            return null;
        }
        ApArticle apArticle = articleService.getById(articleId);
        return ResponseResult.okResult(apArticle);
    }

    @PostMapping("/getArticleConfig")
    public ResponseResult<ApArticleConfig> getArticleConfig(@RequestBody ApArticleConfigDto apArticleConfigDto){
       ApArticleConfig apArticleConfig = apArticleConfigService.getArticleConfig(apArticleConfigDto);
       return ResponseResult.okResult(apArticleConfig);
    }

    @PostMapping("/updateArticleConfig")
    public ResponseResult updateArticleConfig(@RequestBody ApArticleConfigDto apArticleConfigDto){
        apArticleConfigService.updateArticleConfig(apArticleConfigDto);
        return ResponseResult.okResult(null);
    }
}
